//
//  MainView.m
//  YaHooFrame
//
//  Created by dg11185_zal on 14/11/24.
//  Copyright (c) 2014年 dg11185. All rights reserved.
//

#import "MainView.h"

@implementation MainView

@synthesize delegate;

//初始化视图
-(MainView*) initWithFrame:(CGRect)frame withArray:(NSArray*)titleArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:175/255.0 blue:1 alpha:0.8];
        _titleArray = [NSMutableArray arrayWithArray:titleArray];
        _titleLabels = [NSMutableArray array];
        //添加滚动框架
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        //添加子视图
        NSInteger titleCount = _titleArray.count;
        float scrollWidth = _scrollView.frame.size.width;
        float scrollHeight = _scrollView.frame.size.height;
        _scrollView.contentSize = CGSizeMake(scrollWidth*titleCount, scrollHeight);
        for (int i = 0; i < titleCount; i++) {
            //子视图
            CGRect viewFrame = CGRectMake(i*scrollWidth, 0, scrollWidth, scrollHeight);
            NSString *cityName = [_titleArray objectAtIndex:i];
            [self addMainView:viewFrame cityName:cityName];
        }
        
        canScroll = YES;
        
        //每3s刷新一次时间
        [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(changeTitle) userInfo:nil repeats:YES];
    }
    
    return self;
}

//切换滚动状态
-(void) changeScroll{
    canScroll = !canScroll;
    if (!canScroll) {
        _scrollView.scrollEnabled = NO;
    }else{
        _scrollView.scrollEnabled = YES;
    }
}

//左侧栏切换
-(void) showSideView{
    if (self.delegate) {
        [self.delegate showSideView];
    }
}

//通知添加子视图
-(void) addNewView{
    if (self.delegate) {
        [self.delegate addNewView];
    }
}
//根据新城市添加新视图
-(void) addNewCityView:(NSString*)cityName
{
    [_titleArray addObject:cityName];
    //添加子视图
    NSInteger titleCount = _titleArray.count;
    float scrollWidth = _scrollView.frame.size.width;
    float scrollHeight = _scrollView.frame.size.height;
    _scrollView.contentSize = CGSizeMake(scrollWidth*titleCount, scrollHeight);

    CGRect viewFrame = CGRectMake((titleCount-1)*scrollWidth, 0, scrollWidth, scrollHeight);
    [self addMainView:viewFrame cityName:cityName];
    [_scrollView setContentOffset:CGPointMake((titleCount-1)*scrollWidth, 0) animated:NO];
}

//更换界面
-(void) changeView:(NSInteger)cityIndex
{
    float scrollWidth = _scrollView.frame.size.width;
    [_scrollView setContentOffset:CGPointMake(cityIndex*scrollWidth, 0) animated:NO];
}

#pragma mark Delegate
/***********************UIScrollViewDelegate********************/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //int viewIndex = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
    //NSLog(@"viewIndex=%d",viewIndex);
}


#pragma mark -Helper
//向滚动视图添加视图(城市视图)
-(void) addMainView:(CGRect)frame cityName:(NSString*)cityName
{
    //添加子视图
    UIView *mainView = [[UIView alloc] initWithFrame:frame];
    float randomNum = (float)(arc4random()%100)/100;
    NSLog(@"randomNum=%f",randomNum);
    mainView.backgroundColor = [UIColor colorWithRed:0 green:175/255.0 blue:randomNum alpha:0.8];
    //添加顶部栏
    float topViewHeight = 64;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), topViewHeight)];
    topView.backgroundColor = [UIColor clearColor];
    [self addSubview:topView];
    
    float horizonPadding = 10;
    float buttonHeight = 40;
    float buttonWidth = 60;
    //向顶部栏添加左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(horizonPadding, (topViewHeight-buttonHeight)/2, buttonWidth, buttonHeight)];
    [leftButton setImage:[UIImage imageNamed:@"threelines.png"] forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 15, 25);
    [leftButton addTarget:self action:@selector(showSideView) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:leftButton];
    //向顶部栏添加标题标签
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(horizonPadding+buttonWidth, 0, topView.frame.size.width-horizonPadding*2-buttonWidth*2, topViewHeight)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.numberOfLines = 0;
    [topView addSubview:titleLabel];
    [_titleLabels addObject:titleLabel];
    //显示城市和时间
    NSDate *senddate = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString *timeString = [dateformatter stringFromDate:senddate];
    titleLabel.text = [NSString stringWithFormat:@"%@\n%@",cityName,timeString];
    //向顶部栏添加右侧按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x+titleLabel.frame.size.width, (topViewHeight-buttonHeight)/2, buttonWidth, buttonHeight)];
    [rightButton setImage:[UIImage imageNamed:@"common_add.png"] forState:UIControlStateNormal];
    rightButton.imageEdgeInsets = UIEdgeInsetsMake(5, 33, 15, 5);
    [rightButton addTarget:self action:@selector(addNewView) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:rightButton];
    
    [mainView addSubview:topView];
    [_scrollView addSubview:mainView];
}

//更新标题时间
-(void) changeTitle{
    //显示城市和时间
    NSDate *senddate = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString *timeString = [dateformatter stringFromDate:senddate];
    NSInteger titleCount = _titleLabels.count;
    for(int i=0; i<titleCount; i++){
        UILabel *titleLabel = [_titleLabels objectAtIndex:i];
        titleLabel.text = [NSString stringWithFormat:@"%@\n%@",[_titleArray objectAtIndex:i],timeString];
    }
}


@end
